package com.web.forum.ao.impl;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.mysteel.util.ObjectUtils;

import com.base.forum.entity.Forum;
import com.base.forum.service.ForumService;
import com.base.user.entity.Member;
import com.base.user.service.MemberService;
import com.web.forum.ao.ForumAo;
import com.web.kongjian.ao.utils.KongjianUtils;

@Service("forumAo")
public class ForumAoImpl implements ForumAo
{
	@Autowired
	private MemberService memberService;
	@Autowired
	private ForumService forumService;
	
	@Override
	public String news(HttpServletRequest request, String name, String label)
	{
		Forum f = new Forum ();
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		Member m = memberService.getMemberById (memberId);
		if(ObjectUtils.notNull (m))
		{
			f.setCreateTime (System.currentTimeMillis ());
			f.setCreatorMemberId (memberId);
			f.setCreatorName (m.getNickName ());
			f.setLabel (label);
			f.setName (name);
			f.setMemberId (m.getId ()+",");
			f.setNumOfMember (1);
		}
		
		return forumService.Create (f);
	}

	@Override
	public void join(HttpServletRequest request, long forumId)
	{

		long memberId = KongjianUtils.getMemberIdByCookie (request );
		Forum f =  forumService.getById (forumId);
		if(ObjectUtils.notNull (f))
		{
			String memberStr = f.getMemberId ();
			if(memberStr.indexOf (memberId+",")==-1)
			{
				f.setMemberId (f.getMemberId ()+memberId+",");
				f.setNumOfMember (f.getNumOfMember ()+1);
				
				forumService.update (f);
			}
		}
	}
}
